package com.sci.web.controller.gateway.v2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Page;
import com.sci.config.MyConstants;
import com.sci.web.controller.BaseController;
import com.sci.web.model.genmodel.Blog;
import com.sci.web.model.genmodel.User;
import com.sci.web.service.gateway.v2.BlogService;
import com.sci.web.util.DateUtil;
import com.sci.web.util.StringUtil;

public class BlogController extends BaseController {
	
	
	private BlogService blogService = new BlogService();
	
	
	
	/**
	* @Title: bloglist
	* @Description: 显示博客列表页面
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	public void getBlogList(){
		
		Integer p = getParaToInt("p");
		if(p==null){
			p=1;
		}
		User user = super.getSessionByUser();
		Page<Blog> blogPageInate = blogService.getBlogPageInate(p);
		List<Blog> blogList = new ArrayList<Blog>();
		for (Blog blog : blogPageInate.getList()) {
			String content = blog.getContent();
			if(content.length()>25){
				content = content.substring(0, 24);
			}
			String pushTime = blog.getPushTime();
			if(StringUtil.isEmpty(pushTime)){
				pushTime = blog.getUpdateTime();
			}
			blog.setPushTime(DateUtil.getCurrDateTimeCn(pushTime));
			blog.setContent("[内容隐藏,点击查看详情]");
			blogList.add(blog);
		}
		if(user!=null){
			
		}
		MyConstants.log.info("====>"+blogPageInate.getList().size());
		setAttr("bloglist", new Page<Blog>(blogList, blogPageInate.getPageNumber(), 
				blogPageInate.getPageSize(), blogPageInate.getTotalPage(), blogPageInate.getTotalRow()));
		render("blog_list.jsp");
	}
	
	
	
	
	
	
	
	
	
	
	/**
	* @Title: insertBlog
	* @Description: 添加博客内容
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	public void insertBlog(){
		String reqStr = getPara("reqStr");
		Map<String  , Object> resMap = new HashMap<String, Object>();
		User user = super.getSessionByUser();
		if(user==null){
			redirect("/v2/tologin");
		}else{
			System.out.println(reqStr);
			Map<String , String> map = null;
			try {
				 map=(Map<String , String>) JSONObject.parseObject(reqStr, Map.class);
			} catch (Exception e) {
				// TODO: handle exception
			}
			if(map!=null){
				String serialId = map.get("serialId");
				int insertBlog = blogService.insertBlog(map, user);
				if(insertBlog==1){
					
					StringBuffer requestURL = getRequest().getRequestURL();
					
					resMap.put("id", serialId);
					resMap.put("addr", requestURL.toString().replace("insertBlog", "getBlogInfo"));
					
					success(resMap);
				}else if(insertBlog==2){
					error("请勿重复添加数据！");
				}else{
					error("error");
				}
			}else{
				error("请确认参数正确！");
			}
		}
	}
	
	
	
	
	public void getBlogInfo(){
		String id = getPara("id");
		User user = super.getSessionByUser();
		user = new User();
		if(user==null){
			redirect("/v2/tologin");
		}else{
			//System.out.println(reqStr);
			Blog blogInfo = blogService.getBlogInfo(id);
			if(blogInfo!=null){
				String pushTime = blogInfo.getPushTime();
				if(StringUtil.isEmpty(pushTime)){
					pushTime = blogInfo.getUpdateTime();
				}
				blogInfo.setPushTime(DateUtil.getCurrDateTimeCn(pushTime));
				setAttr("blogInfo", blogInfo);
				blogService.updateBlogClickNum(id, blogInfo.getClickNum());
				render("blog_info.jsp");
			}
		}
	}
	
	
	
	
	
	

	
	
	
	
	
	
	

}
